package com.mockapi.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mockapi.entity.ResponseTemplate;
import com.mockapi.mapper.ResponseTemplateMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 响应模板服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ResponseTemplateService {

    private final ResponseTemplateMapper responseTemplateMapper;

    /**
     * 分页查询模板
     */
    public Page<ResponseTemplate> page(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<ResponseTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null, ResponseTemplate::getName, name)
                .orderByDesc(ResponseTemplate::getCreatedAt);
        
        return responseTemplateMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    /**
     * 查询所有模板
     */
    public List<ResponseTemplate> list() {
        LambdaQueryWrapper<ResponseTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ResponseTemplate::getCreatedAt);
        
        return responseTemplateMapper.selectList(wrapper);
    }

    /**
     * 根据ID查询模板
     */
    public ResponseTemplate getById(Long id) {
        return responseTemplateMapper.selectById(id);
    }

    /**
     * 保存模板
     */
    public boolean save(ResponseTemplate template) {
        return responseTemplateMapper.insert(template) > 0;
    }

    /**
     * 更新模板
     */
    public boolean update(ResponseTemplate template) {
        return responseTemplateMapper.updateById(template) > 0;
    }

    /**
     * 删除模板
     */
    public boolean delete(Long id) {
        return responseTemplateMapper.deleteById(id) > 0;
    }

    /**
     * 获取模板总数
     */
    public long getCount() {
        return responseTemplateMapper.selectCount(null);
    }
} 